package frame;

import handler.ClientHandler;

import java.awt.Point;
import java.awt.event.MouseEvent;
import java.awt.event.MouseListener;

import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.border.BevelBorder;
import javax.swing.JTextField;
import javax.swing.JButton;
import javax.swing.border.CompoundBorder;
import javax.swing.ImageIcon;
import javax.swing.JScrollPane;
import javax.swing.JTextArea;

import bean.Client;
import bean.RequestData;

import com.gmail.dengtao.joe.transport.SocketConnector;

import java.awt.event.ActionListener;
import java.awt.event.ActionEvent;
import java.text.SimpleDateFormat;
import java.util.Date;

public class ClientFrame extends JFrame {
	private JPanel contentPane;
	private JTextField massageTextField;
	private JLabel selfLabel;
	private JLabel otherLabel;
	private JButton giveInButton;
	private JButton readyButton;
	private JButton sendButton;
	private JButton clearButton;
	private JButton returnButton;
	private JTextArea messageTextArea;

	private int[][] chess = new int[19][19];
	private Client client;
	private int name;

	private ClientPanel chessPanel;
	private ClientHandler clientHandler;

	private String host = "172.17.41.181";
	private int port = 8088;


	public static void main(String[] args) {
		new ClientFrame();
	}

	public ClientFrame(String host, int port) {
		this.host = host;
		this.port = port;
		initialize();
		this.setVisible(true);
	}

	public ClientFrame() {
		initialize();
		this.setVisible(true);
	}

	private void initialize() {
		setTitle("逗比大作战");
		setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
		setBounds(100, 100, 1009, 691);
		contentPane = new JPanel();
		contentPane.setBorder(new CompoundBorder());
		setContentPane(contentPane);
		contentPane.setLayout(null);

		chessPanel = new ClientPanel();
		chessPanel.setBorder(new BevelBorder(BevelBorder.RAISED, null, null,
				null, null));
		chessPanel.setBounds(3, 3, 600, 600);
		contentPane.add(chessPanel);

		massageTextField = new JTextField();
		massageTextField.setBounds(634, 571, 349, 29);
		contentPane.add(massageTextField);
		massageTextField.setColumns(10);

		selfLabel = new JLabel("自己");
		selfLabel.setIcon(new ImageIcon("resource\\imageBlack.png"));
		selfLabel.setToolTipText("");
		selfLabel.setBounds(634, 10, 349, 40);
		contentPane.add(selfLabel);

		otherLabel = new JLabel("对手");
		otherLabel.setIcon(new ImageIcon("resource\\imageWhite.png"));
		otherLabel.setBounds(634, 58, 349, 40);
		contentPane.add(otherLabel);

		JLabel lblNewLabel_2 = new JLabel("消息记录");
		lblNewLabel_2.setBounds(634, 113, 54, 15);
		contentPane.add(lblNewLabel_2);

		giveInButton = new JButton("投降");
		giveInButton.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
			}
		});
		giveInButton.setBounds(507, 624, 93, 23);
		contentPane.add(giveInButton);

		readyButton = new JButton("准备");
		readyButton.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				String content = massageTextField.getText();
				RequestData requestData = new RequestData();
				requestData.setCommand(RequestData.REQUEST_GET_READY);
				requestData.setContent(content);
				requestData.setIdentify(client.getIdentify());
				requestData.setTime(new Date());
				request(requestData);
			}
		});
		readyButton.setBounds(402, 624, 93, 23);
		contentPane.add(readyButton);

		sendButton = new JButton("发送消息");
		sendButton.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				String content = massageTextField.getText();
				RequestData requestData = new RequestData();
				requestData.setCommand(RequestData.CHAT);
				requestData.setContent(content);
				requestData.setIdentify(client.getIdentify());
				requestData.setTime(new Date());

				SimpleDateFormat sf = new SimpleDateFormat(
						"yyyy-MM-dd HH:mm:ss");
				String dateTime = sf.format(new Date());
				messageTextArea.append("" + dateTime + " " + "自己" + ":\n"
						+ content + "\n");

				request(requestData);
			}
		});
		sendButton.setBounds(637, 624, 93, 23);
		contentPane.add(sendButton);

		clearButton = new JButton("清除记录");
		clearButton.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
			}
		});
		clearButton.setBounds(890, 624, 93, 23);
		contentPane.add(clearButton);

		returnButton = new JButton("返回大厅");
		returnButton.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
			}
		});
		returnButton.setBounds(10, 624, 93, 23);
		contentPane.add(returnButton);

		JScrollPane scrollPane = new JScrollPane();
		scrollPane.setBounds(634, 138, 349, 423);
		contentPane.add(scrollPane);

		messageTextArea = new JTextArea();
		scrollPane.setViewportView(messageTextArea);

		chessPanel.addMouseListener(new MouseListener() {
			@Override
			public void mouseClicked(MouseEvent e) {
				if (client.getStatus() == Client.TURN) {
					Point point = e.getPoint();
					Point pointNew = dingwei(point.x, point.y);
					if(isEmpty(pointNew,chess)){
						chess[pointNew.x][pointNew.y] = client.getIdentify();
						chessPanel.show(chess,pointNew.x,pointNew.y);
						showMessage("self", "落子", new Date());
						RequestData requestData = new RequestData();
						requestData.setCommand(RequestData.REQUEST_PLAY);
						requestData.setIdentify(client.getIdentify());
						requestData.setPosX(pointNew.x);
						requestData.setPosY(pointNew.y);
						requestData.setTime(new Date());
						request(requestData);
						client.setStatus(Client.FIGHTING);
					}					
				}
			}

			@Override
			public void mouseEntered(MouseEvent arg0) {
			}

			@Override
			public void mouseExited(MouseEvent arg0) {
			}

			@Override
			public void mousePressed(MouseEvent arg0) {
			}

			@Override
			public void mouseReleased(MouseEvent arg0) {

			}
		});
		try {
			onInitializeOver();
		} catch (Exception e1) {
			showMessage("系统消息", "网络连接错误", new Date());
		}
	}

	private void onInitializeOver() throws Exception {
		final SocketConnector connector = new SocketConnector();
		// sometimes, you should init before...
		connector.init();
		// Custom setting...
		connector.setSelectTimeout(10000);
		connector.setReceiveBufferSize(1024);
		connector.setSendBufferSize(1024);
		// Inject the biz handler.
		ClientHandler clientHandler = new ClientHandler();
		clientHandler.setClientFrame(this);
		this.clientHandler = clientHandler;
		connector.setHandler(clientHandler);
		// Open connection to host:port
		// Thread will be blocked in here.
		new Thread(new Runnable() {
			@Override
			public void run() {
				try {
					connector.open(host, port);
				} catch (Exception e) {
					// TODO Auto-generated catch block
					e.printStackTrace();
				}
			}
		}).start();

	}

	public Point dingwei(int x, int y) {
		Point point = new Point();
		int size = 30;
		int ox = x / size;
		int mx = x % size;
		int oy = y / size;
		int my = y % size;
		if (mx < 15) {
			point.x = ox;
		} else {
			point.x = (ox + 1);
		}
		if (my < 15) {
			point.y = oy;
		} else {
			point.y = (oy + 1);
		}
		return point;
	}

	public void showMessage(String name, String message, Date time) {
		SimpleDateFormat sf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
		String dateTime = sf.format(new Date());
		messageTextArea.append("" + dateTime + " " + name + ":\n" + message
				+ "\n");
		messageTextArea.setCaretPosition(messageTextArea.getText().length());  

	}

	public void request(RequestData requestData) {
		clientHandler.sendMessage(requestData);
	}

	public void showChess(int[][] chess,int x,int y) {
		this.chess = chess;
		chessPanel.show(chess,x,y);
	}

	public Client getClient() {
		return client;
	}

	public void setClient(Client client) {
		this.client = client;
	}
	public boolean isEmpty(Point point,int[][] chess){
		int size=19;
		if(chess[point.x][point.y]==0){
			return true;
		}
		return false;
		
	}
}
